<template>
  <div>
    <el-row>
      <el-col>
        <el-select v-model="selectYear" style="width:100px; marginRight:5px" size="mini" @change="updateCalendar">
          <el-option
            v-for="item in years"
            :key="item"
            :label="item"
            :value="item"
          />
        </el-select>
        <el-select v-model="selectMonth" style="width:70px" size="mini" @change="updateCalendar">
          <el-option
            v-for="item in 12"
            :key="item"
            :label="item"
            :value="item"
          />
        </el-select>
      </el-col>
    </el-row>
    <el-calendar v-model="currentDate">
      <template v-slot:dateCell="{date,data}">
        {{ data.day.split('-')[2] }}
        <span v-if="isRest(date)" class="rest">休</span>
      </template>
    </el-calendar>
  </div>
</template>

<script>
export default {
  data() {
    return {
      years: [],
      selectYear: new Date().getFullYear(),
      selectMonth: new Date().getMonth() + 1,
      currentDate: new Date()
    }
  },
  created() {
    // Array(11) 生成一个长度为11的伪数组 fill方法可填充内容 map方法对每一项进行改造
    // this.years = Array(11).fill('').map((item, index) => this.selectYear - 5 + index)
    // Array.from(伪数组,callback(item,index)) 将伪数组转为数组
    this.years = Array.from(Array(11), (value, index) => this.selectYear - 5 + index)
  },
  methods: {
    // 根据日期判断是否显示休息
    isRest(date) {
      return date.getDay() === 6 || date.getDay() === 0
    },
    // 更新日历的回调
    updateCalendar() {
      const date = this.selectYear + '-' + this.selectMonth
      this.currentDate = new Date(date)
    }
  }
}
</script>

<style scoped lang="scss">
.el-row .el-col {
  text-align: right;
}
 ::v-deep .el-calendar-day {
  height:  auto;
  text-align: center;
  line-height: 30px;
 }
 ::v-deep .el-calendar-table__row td,::v-deep .el-calendar-table tr td:first-child,  ::v-deep .el-calendar-table__row td.prev{
  border:none;
 }
.date-content {
  height: 40px;
  text-align: center;
  line-height: 40px;
  font-size: 14px;
}
.date-content .rest {
  color: #fff;
  border-radius: 50%;
  background: rgb(250, 124, 77);
  width: 20px;
  height: 20px;
  line-height: 20px;
  display: inline-block;
  font-size: 12px;
  margin-left: 10px;
}
.date-content .text{
  width: 20px;
  height: 20px;
  line-height: 20px;
 display: inline-block;

}

::v-deep thead {
  th {
    font-size: 14px;
    font-weight: bold;
  }
}
 ::v-deep .el-calendar-table td.is-selected .text{
   background: #409eff;
   color: #fff;
   border-radius: 50%;
 }
 ::v-deep .el-calendar__header {
   display: none
 }
 .rest {
    background: #fa7c4d;
    display: inline-block;
    position: relative;
    top: -1px;
    width: 20px;
    height: 20px;
    line-height: 20px;
    font-size: 10px;
    border-radius: 50%;
    color: #fff;
 }
</style>
